<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Logic\AppointmentLogic;

use Illuminate\Support\Facades\Validator;
use App\Models\Appointment;

class AppointmentController extends ApiController
{
    public function __construct()
    {
    }
    
    // 获取预约的数据
    public function index(Request $request)
    {
        $where = [
            //'uid' =>\Auth::user()->id,
            'uid' =>8,
        ];
        $apotData = AppointmenttLogic::getApoList($where);
        return $this->success($apoData);
    }
    
    // 添加预约
    public function add(Request $request)
    {
        //         if (empty(\Auth::user()->id)) {
        //             $user_id = 0;
        //         } else {
        //             $user_id = \Auth::user()->id;
        //         }
        
        $user_id = 8;
        
        // 参数校验
        $validator = Validator::make($request->all(),
            [
                'userName' => 'required',
                'mobile' => 'required|numeric',
                'at_time' => 'required'
            ],
            [
                'userName.required' => '预约姓名参数缺失',
                'mobile.required' => '预约电话参数缺失',
                'at_time.required' => '预约时间参数缺失',
            ]
            );
        
        if ($validator->fails()) {
            return $this->failed($validator->errors(), 403);
        }
        
        
        $re = AppointmentLogic::addApo($request);
        if($re){
            $apoCount = Appointment::getApoCount(['uid' => $user_id]);
            return  $this->success(['apoCount'=>$apoCount]);
        }
        return $this->failed('预约失败', 201);
    }
    
}